﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TorchView.GameContent.Data;

namespace TorchView.GameContent.Units
{
  public sealed class Effect : IDataSerializable
  {
    public const string BLOCK_NAME = "EFFECT";

    public Effect()
    {
      Properties = new Dictionary<string, float>(2);
    }

    #region IDataSerializable Members

    public DataBlock MakeDataBlock()
    {
      var block = Data == null ? new DataBlock(BLOCK_NAME) : Data;

      block.SetOrClearPropertyValue(Name != "", "NAME", Name);
      block.SetOrClearPropertyValue(Activation != "", "ACTIVATION", Activation);
      block.SetOrClearPropertyValue(Duration != "", "DURATION", Duration);
      block.SetOrClearPropertyValue(Level != 0, "LEVEL", Level);
      block.SetOrClearPropertyValue(Type != "", "TYPE", Type);
      block.SetOrClearPropertyValue(DamageType != "", "DAMAGE_TYPE", DamageType);
      block.SetOrClearPropertyValue(ParticleFX != "", "PARTICLE_FX", ParticleFX);
      block.SetOrClearPropertyValue(StatModifyName != "", "STATMODIFYNAME", StatModifyName);
      block.SetOrClearPropertyValue(StatModifyPercent != "", "STATMODIFYPERCENT", StatModifyPercent);
      block.SetOrClearPropertyValue(GraphOverride != "", "GRAPHOVERRIDE", GraphOverride);
      block.SetOrClearPropertyValue(NoGraph, "NOGRAPH", NoGraph);
      block.SetOrClearPropertyValue(UseOwnerLevel, "USEOWNERLEVEL", UseOwnerLevel);
      block.SetOrClearPropertyValue(UnitTheme != "", "UNITTHEME", UnitTheme);
      block.SetOrClearPropertyValue(Save, "SAVE", Save);
      block.SetOrClearPropertyValue(Exclusive, "EXCLUSIVE", Exclusive);
      block.SetOrClearPropertyValue(LinkName != "", "LINK_NAME", LinkName);

      foreach (var property in Properties)
        block.SetPropertyValue(property.Key, property.Value);

      return block;
    }

    public void LoadFromDataBlock(DataBlock block)
    {
      if (block == null)
        throw new ArgumentNullException("block");
      if (block.Name != BLOCK_NAME)
        throw new ArgumentException("Incorrect block type, only " + BLOCK_NAME + " is supported.");

      Data = block;
      Name = block.GetPropertyValue("NAME", "");
      Activation = block.GetPropertyValue("ACTIVATION", "");
      Duration = block.GetPropertyValue("DURATION", "");
      Level = block.GetPropertyValue("LEVEL", 0);
      Type = block.GetPropertyValue("TYPE", "");
      DamageType = block.GetPropertyValue("DAMAGE_TYPE", "");
      ParticleFX = block.GetPropertyValue("PARTICLE_FX", "");
      StatModifyName = block.GetPropertyValue("STATMODIFYNAME", "");
      StatModifyPercent = block.GetPropertyValue("STATMODIFYPERCENT", "");
      GraphOverride = block.GetPropertyValue("GRAPHOVERRIDE", "");
      NoGraph = block.GetPropertyValue("NOGRAPH", false);
      UseOwnerLevel = block.GetPropertyValue("USEOWNERLEVEL", false);
      UnitTheme = block.GetPropertyValue("UNITTHEME", "");
      Save = block.GetPropertyValue("SAVE", false);
      Exclusive = block.GetPropertyValue("EXCLUSIVE", false);
      LinkName = block.GetPropertyValue("LINK_NAME", "");

      // To Do: Add extended properties
    }

    #endregion

    public DataBlock Data { get; set; }
    public Dictionary<string, float> Properties { get; private set; }
    public string Type { get; set; }
    public string Name { get; set; }
    public string Activation { get; set; }
    public string Duration { get; set; }
    public int Level { get; set; }
    public string DamageType { get; set; }
    public string GraphOverride { get; set; }
    public string StatModifyPercent { get; set; }
    public string StatModifyName { get; set; }
    public string ParticleFX { get; set; }
    public bool NoGraph { get; set; }
    public bool UseOwnerLevel { get; set; }
    public string UnitTheme { get; set; }
    public bool Save { get; set; }
    public bool Exclusive { get; set; }
    public string LinkName { get; set; }
  }
}
